(Quick Reference)
save
Purpose
Saves a domain class instance to the database cascading updates to any child instances if required.
Examples
def b = new Book(title:"The Shining")
b.save()
Description
The
save
method informs the persistence context that an instance should be saved or updated. The object will not be persisted immediately unless the
flush
argument is used:
The
save
method returns
null
if
validation failed and the instance was not saved and the instance itself if successful. This allows you to write code like the following:
if( !b.save() ) {
b.errors.each {
println it
}
}
Parameters:
validate
(optional) - Set to false
if validation should be skipped
flush
(optional) - When set to true
flushes the persistent context, hence persisting the object immediately and updating the version
column for optimistic locking
insert
(optional) - When set to true
will force Hibernate to do a SQL INSERT, this is useful in certain situations when legacy databases (such as AS/400) are involved and Hibernate cannot detect whether to do an INSERT or an UPDATE
failOnError
(optional) - When set to true
the save method with throw a grails.validation.ValidationException
if validation fails during a save. This behavior may also be triggered by setting the grails.gorm.failOnError
property in grails-app/conf/Config.groovy
. If the Config property is set and the argument is passed to the method, the method argument will always take precedence. For more details about the config property and other GORM config options, see the GORM Configuration Options section of The User Guide.
deepValidate
(optional) - Determines whether associations of the domain instance should also be validated, i.e. whether validation cascades or not. This is true
by default - set to false
to disable cascading validation.
By default GORM classes are configured for optimistic locking, which is a feature of Hibernate that involves storing a special version number in the table. This value is only updated in the database when the Hibernate session is flushed.